package cn.com.nes.site.controller.system;

import cn.com.nes.mybatis.agent.system.entity.Department;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.vo.system.DepartmentDeleteVO;
import cn.com.nes.site.entity.vo.system.DepartmentSaveVO;
import cn.com.nes.site.entity.vo.system.DepartmentTreeNodeVO;
import cn.com.nes.site.service.system.company.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 部门业务接口
 */
@Slf4j
@Controller
@RequestMapping("/v2/system/department")
public class DepartmentController {

    @Resource
    DepartmentService departmentService;


    /**
     * 新增、编辑部门
     * @param vo 保存部门前端入参模型
     * @return
     */
    @RequestMapping(value = "saveDepartment", method = RequestMethod.POST)
    @ResponseBody
    public PageData saveDepartment(@RequestBody @Validated DepartmentSaveVO vo) throws Exception {
        Department department = departmentService.saveDepartment(vo);
        return PageData.ok("departmentId", department.getDepartmentId());
    }

    /**
     * 删除部门
     * @param vo 删除部门前端入参模型
     * @return
     */
    @RequestMapping(value = "deleteDepartment", method = RequestMethod.POST)
    @ResponseBody
    public PageData deleteDepartment(@RequestBody @Validated DepartmentDeleteVO vo) throws Exception {
        departmentService.deleteDepartment(vo);
        return PageData.ok();
    }

    /**
     * 部门下是否有用户
     * @param departmentId 部门ID
     * @return
     */
    @RequestMapping(value = "departmentHasUsers", method = RequestMethod.GET)
    @ResponseBody
    public PageData departmentHasUsers(@RequestParam(value = "departmentId") String departmentId) throws Exception {
        return PageData.ok("hasUsers", departmentService.departmentHasUsers(departmentId));
    }

    /**
     * 查询部门树
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getDepartmentTreeNode", method = RequestMethod.GET)
    @ResponseBody
    public PageData getDepartmentTreeNode(@Validated DepartmentTreeNodeVO vo) throws Exception {
        return PageData.ok("tree", departmentService.getDepartmentTreeNode(vo));
    }

}
